#include "WxOrientationMark.h"

WxOrientationMark::WxOrientationMark(vtkRenderWindowInteractor* iren,const char* modelfilename)
{
    m_StlReader=NULL;
    //////////////////////////////////////////////////////////////////////////////////////////
    if(itksys::SystemTools::FileExists(modelfilename))
    {
        m_StlReader=vtkSTLReader::New();
        m_StlReader->SetFileName(modelfilename);
        m_StlReader->Update();

        m_ModelMapper = vtkPolyDataMapper::New();
        m_Model = vtkActor::New();
        m_ModelMapper->SetInputConnection(m_StlReader->GetOutputPort());
        m_Model->SetMapper(m_ModelMapper);
        m_Model->SetScale(6);
        m_Model->GetProperty()->SetColor(1.0,0.5,0.5);
    }

	//////////////////////////////////////////////////////////////////////////////////////////
    m_Cube = vtkAnnotatedCubeActor::New();
    m_Cube->SetFaceTextScale( 0.6);
    vtkProperty* property =	m_Cube->GetCubeProperty();
    property->SetColor( 0.1, 0.3, 0.4 );
    m_Cube->SetXPlusFaceText ( "L" );
    m_Cube->SetXMinusFaceText( "R" );
    m_Cube->SetYPlusFaceText ( "P" );
    m_Cube->SetYMinusFaceText( "A" );
    m_Cube->SetZPlusFaceText ( "S" );
    m_Cube->SetZMinusFaceText( "I" );
    property = m_Cube->GetXPlusFaceProperty();
    property->SetColor(1, 1, 1);
    property->SetInterpolationToFlat();
    property = m_Cube->GetXMinusFaceProperty();
    property->SetColor(1, 1, 1);
    property->SetInterpolationToFlat();
    property = m_Cube->GetYPlusFaceProperty();
    property->SetColor(1, 1, 1);
    property->SetInterpolationToFlat();
    property = m_Cube->GetYMinusFaceProperty();
    property->SetColor(1, 1, 1);
    property->SetInterpolationToFlat();
    property = m_Cube->GetZPlusFaceProperty();
    property->SetColor(1, 1, 1);
    property->SetInterpolationToFlat();
    property = m_Cube->GetZMinusFaceProperty();
    property->SetColor(1, 1, 1);
    property->SetInterpolationToFlat();

    //////////////////////////////////
    m_Axes = vtkAxesActor::New();
    m_Axes->SetShaftTypeToCylinder();
    m_Axes->SetXAxisLabelText( "X" );
    m_Axes->SetYAxisLabelText( "Y" );
    m_Axes->SetZAxisLabelText( "Z" );
    m_Axes->SetTotalLength( 1.6, 1.6,1.6 );
    m_Axes->SetCylinderRadius( 0.6 * m_Axes->GetCylinderRadius() );
    m_Axes->SetConeRadius    ( 1.0 * m_Axes->GetConeRadius() );
    m_Axes->SetSphereRadius  ( 1.0 * m_Axes->GetSphereRadius() );
    vtkTextProperty* xprop = m_Axes->GetXAxisCaptionActor2D()->GetCaptionTextProperty();
    xprop->ShadowOff();
    xprop->SetColor(1.0,0.0,0.0);
    xprop->SetFontFamilyToArial();
    vtkTextProperty* yprop = m_Axes->GetYAxisCaptionActor2D()->GetCaptionTextProperty();
    yprop->ShadowOff();
    yprop->SetColor(0.0,1.0,0.0);
    yprop->SetFontFamilyToArial();
    vtkTextProperty* zprop = m_Axes->GetZAxisCaptionActor2D()->GetCaptionTextProperty();
    zprop->ShadowOff();
    zprop->SetColor(0.0,0.0,1.0);
    zprop->SetFontFamilyToArial();
    m_Axes->GetXAxisCaptionActor2D()->GetCaptionTextProperty()->ShallowCopy( xprop );
    m_Axes->GetYAxisCaptionActor2D()->GetCaptionTextProperty()->ShallowCopy( yprop );
    m_Axes->GetZAxisCaptionActor2D()->GetCaptionTextProperty()->ShallowCopy( zprop );

    //////////////////
    m_Assembly = vtkPropAssembly::New();
    m_Assembly->AddPart( m_Axes );
    m_Assembly->AddPart( m_Cube );
    if (m_StlReader!=NULL)
    {
            m_Assembly->AddPart( m_Model);
    }
    /////////////////////
    m_Widget = vtkOrientationMarkerWidget::New();
    m_Widget->SetOrientationMarker( m_Assembly );
    m_Widget->SetInteractor( iren );
    m_Widget->SetViewport( 0.0, 0.0, 0.18, 0.28 );
    m_Widget->EnabledOn();
    m_Widget->InteractiveOff();
}

WxOrientationMark::~WxOrientationMark(void)
{
    if (m_StlReader!=NULL)
	{
		m_StlReader->Delete();
		m_ModelMapper->Delete();
		m_Model->Delete();
	}
    m_Cube->Delete();
    m_Axes->Delete();
    m_Assembly->Delete();
    m_Widget->Delete();
}

void WxOrientationMark::SetModelVisible(bool visible)
{
    if (m_StlReader!=NULL)
    {
        m_Model->SetVisibility(visible);
    }
}

void WxOrientationMark::SetAxesVisible(bool visible)
{
    m_Axes->SetVisibility(visible);
}

void WxOrientationMark::SetCubeVisible(bool visible)
{
    m_Cube->SetVisibility(visible);
}
